cmake_minimum_required(VERSION 3.10)
project(IngenicVideoStreamer)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置编译选项
if(WIN32)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /O2 /D_WIN32_WINNT=0x0601 /DWIN32_LEAN_AND_MEAN")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DUNICODE /D_UNICODE")
else()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -Wextra")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
endif()

# 查找live555库
find_path(LIVE555_INCLUDE_DIR liveMedia.hh
    PATHS
    ../live555/liveMedia/include
    ../live555/BasicUsageEnvironment/include
    ../live555/groupsock/include
    ../live555/UsageEnvironment/include
    /usr/include/live555
    /usr/local/include/live555
)

# 查找君正SDK库
find_path(INGENIC_SDK_INCLUDE_DIR imp_common.h
    PATHS
    ../Ingenic_SDK
    /usr/include/ingenic
    /usr/local/include/ingenic
)

find_library(INGENIC_SDK_LIB libimp
    PATHS
    ../Ingenic_SDK
    /usr/lib
    /usr/local/lib
)

find_library(LIVE555_LIVEMEDIA_LIB liveMedia
    PATHS
    ../live555/liveMedia
    /usr/lib
    /usr/local/lib
)

find_library(LIVE555_GROUPSOCK_LIB groupsock
    PATHS
    ../live555/groupsock
    /usr/lib
    /usr/local/lib
)

find_library(LIVE555_BASICUSAGEENVIRONMENT_LIB BasicUsageEnvironment
    PATHS
    ../live555/BasicUsageEnvironment
    /usr/lib
    /usr/local/lib
)

find_library(LIVE555_USAGEENVIRONMENT_LIB UsageEnvironment
    PATHS
    ../live555/UsageEnvironment
    /usr/lib
    /usr/local/lib
)

# 检查live555是否找到
if(NOT LIVE555_INCLUDE_DIR OR NOT LIVE555_LIVEMEDIA_LIB)
    message(FATAL_ERROR "live555 not found. Please set LIVE555_INCLUDE_DIR and LIVE555_LIB_DIR")
endif()

# 检查君正SDK是否找到
if(NOT INGENIC_SDK_INCLUDE_DIR OR NOT INGENIC_SDK_LIB)
    message(FATAL_ERROR "Ingenic SDK not found. Please set INGENIC_SDK_INCLUDE_DIR and INGENIC_SDK_LIB")
endif()

# 包含目录
include_directories(${LIVE555_INCLUDE_DIR})
include_directories(${INGENIC_SDK_INCLUDE_DIR})
include_directories(.)

# 源文件
set(SOURCES
    main.cpp
    IngenicVideoSource_Real.cpp
    IngenicEncoder.cpp
    compat.cpp
)

# 头文件
set(HEADERS
    IngenicVideoSource.h
    IngenicEncoder.h
    compat.h
)

# 创建可执行文件
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})

# 链接库
if(WIN32)
    target_link_libraries(${PROJECT_NAME}
        ${LIVE555_LIVEMEDIA_LIB}
        ${LIVE555_GROUPSOCK_LIB}
        ${LIVE555_BASICUSAGEENVIRONMENT_LIB}
        ${LIVE555_USAGEENVIRONMENT_LIB}
        ${INGENIC_SDK_LIB}
        ws2_32
        ole32
        oleaut32
        mfplat
        mfuuid
        mfreadwrite
    )
else()
    target_link_libraries(${PROJECT_NAME}
        ${LIVE555_LIVEMEDIA_LIB}
        ${LIVE555_GROUPSOCK_LIB}
        ${LIVE555_BASICUSAGEENVIRONMENT_LIB}
        ${LIVE555_USAGEENVIRONMENT_LIB}
        ${INGENIC_SDK_LIB}
        pthread
        rt
        dl
        m
    )
endif()

# 设置输出目录
set_target_properties(${PROJECT_NAME} PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)

# 安装规则
install(TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION bin
)

# 打印配置信息
message(STATUS "Live555 include dir: ${LIVE555_INCLUDE_DIR}")
message(STATUS "Live555 libraries:")
message(STATUS "  liveMedia: ${LIVE555_LIVEMEDIA_LIB}")
message(STATUS "  groupsock: ${LIVE555_GROUPSOCK_LIB}")
message(STATUS "  BasicUsageEnvironment: ${LIVE555_BASICUSAGEENVIRONMENT_LIB}")
message(STATUS "  UsageEnvironment: ${LIVE555_USAGEENVIRONMENT_LIB}")
